package view;

import controller.AccountValidation;
import controller.OperationFile;
import controller.ToArray;
import model.ScanRead;

import java.io.IOException;
import java.util.ArrayList;

/**
 * description: 用户信息类
 * author: 时间
 * time: 2022-01-11-16:23
 */
public class MyInformationView extends ViewRun implements Rules{
    // 显示包括，学生(小刚-男-21-201920321243-19级)
    // 教师(小红-女-31-2009203212-6-讲师)信息
    // 当前预约信息
    @Override
    public void views() throws IOException {
        OperationFile operationFile = new OperationFile();
        ToArray toArray = new ToArray();
        System.out.println("***************************我的信息***************************");
        StringBuffer stringBuffer1 = operationFile.readStuAndTea(LoginView.account, Rules.pattern2);
        String[] strs1 = stringBuffer1.toString().split("-");
        if (strs1[3].length() == 12)
        System.out.println("姓名:" + strs1[0] + "\t性别:" + strs1[1] + "\t年龄:" +
                strs1[2] + "\t学号:" + strs1[3] + "\t年级:" + strs1[4] + "\n");
        else {
            System.out.println("姓名:" + strs1[0] + "\t性别:" + strs1[1] + "\t年龄:" +
                    strs1[2] + "\t工号:" + strs1[3] + "\t教龄:" + strs1[4] +
                    "\t职位:" + strs1[5] + "\n");
        }
        if (!AccountValidation.orderExist(LoginView.account, Rules.pattern1)){
            System.out.println("目前还没有预约信息");
        }else {
            System.out.println("预约信息:");
            System.out.println("教室号\t\t教室类型\t\t预约日期\t\t预约时间\t\t\t预留手机号\t\t备注信息");
            ArrayList<StringBuffer> stringBuffer2 = operationFile.readOrderInformation(LoginView.account, Rules.pattern2);
            for(int i = 0; i< stringBuffer2.size(); i++){
                String[] strs2 = stringBuffer2.get(i).toString().split("-");

                System.out.println(strs2[1] + "\t\t" + strs2[2] +"\t\t" + strs2[7] + "\t\t" + strs2[3] +
                        "~" + strs2[4] + "\t\t" + strs2[5] + "\t\t" + strs2[6] );

            }



        }
        System.out.println("\t\t1-取消预约\t\t2-返回");
        System.out.println("*************************************************************");
        System.out.print("请选择：");
        char chr = ScanRead.readOptionTwo();
        switch (chr){
            case '1':
                deleteOrder();
                break;
            case '2':
                exit();
                break;
        }
    }

    // 取消预约(需要二次判断是否取消预约)
    private void deleteOrder() throws IOException {
        OperationFile operationFile = new OperationFile();
//        System.out.print("请输入要取消预约的日期(格式1.15)：");
//        String date = ScanRead.readOrderDate();
        while (true){
            System.out.print("请输入需要取消的教室号：");
            String number = ScanRead.readClassNum();
            if(!AccountValidation.orderClassExist(number, pattern2)){
                System.out.println("该教室号不存在，请重新输入");
                continue;
            }
            System.out.print("确定取消预约(y/n)：");
            char chr = ScanRead.readSelection();
            if (chr == 'Y'){
                //把日期也传进去
                operationFile.deleteOrderClassroom(number);  //删除预约信息
                if (number.charAt(0) == 'A'){
                    operationFile.updateOrderClassroom(number, number + "-" + CLASS1 + "-" + COUNT1
                            + "-" + STATE1 + "-" + "无");
                }
                if (number.charAt(0) == 'B'){
                    operationFile.updateOrderClassroom(number, number + "-" + CLASS2 + "-" + COUNT2
                            + "-" + STATE1 + "-" + "无");
                }
                if (number.charAt(0) == 'C'){
                    operationFile.updateOrderClassroom(number, number + "-" + CLASS2 + "-" + COUNT2
                            + "-" + STATE1 + "-" + "无");
                }
                System.out.println("取消成功");
                ScanRead.readReturn();
                break;
            }
        }

    }

    // 将flags置1
    @Override
    public void exit() {
        flags = 1;
    }
}
